<?php


namespace app\services;


use app\models\Orders;
use app\tools\T;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;

class OrdersService extends Services {
    protected $order_sn;
    protected $price;
    protected $_Users;
    protected $_SellersUsers;
    protected $Product;
    protected $_UsersProduct;

    /**
     * @return mixed
     */
    public function getOrderSn() {
        return $this->order_sn;
    }

    /**
     * @param mixed $order_sn
     */
    public function setOrderSn($order_sn): void {
        $this->order_sn = $order_sn;
    }

    /**
     * @return mixed
     */
    public function getPrice() {
        return $this->price;
    }

    /**
     * @param mixed $price
     */
    public function setPrice($price): void {
        $this->price = $price;
    }

    /**
     * @return mixed
     */
    public function getUsers() {
        return $this->_Users;
    }

    /**
     * @param mixed $Users
     */
    public function setUsers($Users): void {
        $this->_Users = $Users;
    }

    /**
     * @return mixed
     */
    public function getSellersUsers() {
        return $this->_SellersUsers;
    }

    /**
     * @param mixed $SellersUsers
     */
    public function setSellersUsers($SellersUsers): void {
        $this->_SellersUsers = $SellersUsers;
    }

    /**
     * @return mixed
     */
    public function getProduct() {
        return $this->Product;
    }

    /**
     * @param mixed $Product
     */
    public function setProduct($Product): void {
        $this->Product = $Product;
    }

    /**
     * @return mixed
     */
    public function getUsersProduct() {
        return $this->_UsersProduct;
    }

    /**
     * @param mixed $UsersProduct
     */
    public function setUsersProduct($UsersProduct): void {
        $this->_UsersProduct = $UsersProduct;
    }


    public function createOrders() {
        $Orders = new Orders();
        $Orders->order_sn = T::createOrderSn();
        $Orders->price = !empty($this->_UsersProduct) ? $this->_UsersProduct->trad_price : $this->Product->min_num;
        $Orders->buy_user_id = $this->_Users->id;
        $Orders->sellers_user_id = $this->_SellersUsers->id ?? 0;
        $Orders->product_id = $this->Product->id;
        $Orders->status = 1;
        $Orders->ctime = date("Y-m-d H:i:s");
        $Orders->utime = date("Y-m-d H:i:s");
        $Orders->users_product_id = !empty($this->_UsersProduct) ? $this->_UsersProduct->id : 0;
        if ($Orders->save()) {
            $AppointProduct = ArrayHelper::toArray($this->_Users->appointProduct);
            $AppointProductArr = array_keys(array_column($AppointProduct, null, 'product_id'));
            if (!in_array($this->Product->id, $AppointProductArr)) {
                $this->on(self::ON_PAY_SUCCESS,
                    [(new UsersService()), 'onPaySuccess'],
                    [
                        'Users' => $this->_Users,
                        'product_energy' => $this->Product->un_app_energy,
                    ]
                );

                $this->on(self::ON_PAY_SUCCESS,
                    [(new EnergyLogsService()), 'onPaySuccess'],
                    [
                        'type' => 3,
                        'energy_num' => $this->Product->un_app_energy,
                        'user_id' => $this->_Users->id,
                    ]
                );

                if (empty($this->trigger(self::ON_PAY_SUCCESS)))
                    return true;
                else
                    return false;
            }
            return true;
        }else{
            T::getRedis()->set('Orders:CreateOrder:Logs',Json::encode($Orders->getErrors()));
            T::getRedis()->set('Orders:CreateOrder:ErrorLogs',Json::encode($Orders->errors));
            return false;
        }
    }

    public function getOrderById() {
        return Orders::findOne($this->Params);
    }
}